package br.com.viewcar.web.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.faces.model.SelectItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import br.com.viewcar.entity.Modelo;
import br.com.viewcar.service.ModeloService;

@Scope("session")
@Component("modeloBean")
public class ModeloBean extends EntityBean<Integer, Modelo> {

	@Autowired
	private ModeloService service;

	protected Integer retrieveEntityId(Modelo entity) {

		return entity.getId();
	}

	protected ModeloService retrieveEntityService() {

		return this.service;
	}

	protected Modelo createNewEntity() {

		return new Modelo();
	}

	public List<SelectItem> getListaModelos() {

		List<SelectItem> result = new ArrayList<SelectItem>();

		List<Modelo> modelos = service.retrieveAll();

		Collections.sort(modelos, new Comparator<Modelo>() {

			public int compare(Modelo o1, Modelo o2) {

				return o1.getDescricao().compareTo(o2.getDescricao());

			}});

		for (Modelo modelo : modelos) {

			result.add(new SelectItem(modelo.getId(), modelo.getDescricao()));
		}

		return result;
	}


}
